package com.health.dao;

import com.health.pojo.ExamineOrder;
import com.health.pojo.HealthAttributes;
import com.health.pojo.Users;
import com.health.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    /**
     * 查询账户和密码
     */
    public String selectByUser(Users user) {
        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from users where user_account = ? and user_password = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserAccount());
            preparedStatement.setString(2, user.getUserPassword());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                user.setUserId(resultSet.getInt("user_id"));
                user.setUserAccount(resultSet.getString("user_account"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserGender(resultSet.getString("user_gender"));
                user.setUserImg(resultSet.getString("user_img"));
                if ("root".equals(user.getUserAccount())) {
                    return "admin";
                }
                return "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");
            return "systemError";
        }
        return "false";
    }

    /**
     * 查询出所有用户
     */
    public List<Users> selectUserId() {
        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from users";
        List<Users> usersList = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Users user = new Users();
                user.setUserId(resultSet.getInt("user_id"));
                user.setUserAccount(resultSet.getString("user_account"));
                user.setUserName(resultSet.getString("user_name"));
                user.setUserPassword(resultSet.getString("user_password"));
                user.setUserGender(resultSet.getString("user_gender"));
                user.setUserImg(resultSet.getString("user_img"));
                usersList.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");
        }
        return usersList;
    }

    /**
     * 根据用户id查询出所订的套餐信息
     */
    public List<Users> selectDataById(int userId) {

        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        List<Users> usersList = new ArrayList<>();
        String sql = "select * from examine_order,users_examine where examine_order.examine_id = users_examine.examine_id and user_id = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Users user = new Users();
                List<ExamineOrder> examineOrderList = new ArrayList<>();
                ExamineOrder examineOrder = new ExamineOrder();
                user.setUserId(resultSet.getInt("user_id"));
                examineOrder.setExamineId(resultSet.getInt("examine_id"));
                examineOrder.setExamineCombo(resultSet.getString("examine_combo"));
                examineOrder.setExamineDate(resultSet.getString("examine_date"));
                examineOrderList.add(examineOrder);
                user.setExamineOrders(examineOrderList);
                usersList.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");

        }
        return usersList;
    }

    /**
     * 根据用户选中的套餐查询出体检项信息
     */
    public List<Users> selectDataByCombo(int userId, int examineId, String examineDate) {
        Connection connection = ConnectionUtil.getConnection();
        PreparedStatement preparedStatement = null;
        List<Users> usersList = new ArrayList<>();
        String sql = "select * from examine_order,order_attribute,health_attribute,users_examine\n" +
                "where examine_order.examine_id = order_attribute.examine_id and order_attribute.examine_id = users_examine.examine_id and order_attribute.attribute_id = health_attribute.attribute_id\n" +
                "and examine_order.examine_id =? and user_id=? and examine_date=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, examineId);
            preparedStatement.setInt(2, userId);
            preparedStatement.setString(3, examineDate);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Users user = new Users();
                List<ExamineOrder> examineOrderList = new ArrayList<>();
                ExamineOrder examineOrder = new ExamineOrder();
                List<HealthAttributes> healthAttributesList = new ArrayList<>();
                HealthAttributes healthAttributes = new HealthAttributes();

                user.setUserId(resultSet.getInt("user_id"));
                examineOrder.setExamineId(resultSet.getInt("examine_id"));
                examineOrder.setExamineCombo(resultSet.getString("examine_combo"));
                examineOrder.setExamineDate(resultSet.getString("examine_date"));
                healthAttributes.setAttributeName(resultSet.getString("attribute_name"));
                healthAttributes.setAttributeId(resultSet.getInt("attribute_id"));
                healthAttributesList.add(healthAttributes);
                examineOrderList.add(examineOrder);
                user.setExamineOrders(examineOrderList);
                user.setHealthAttributesList(healthAttributesList);
                usersList.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SQL错误！");

        }
        return usersList;
    }
}
